#include "additem.h"
#include "ui_additem.h"

AddItem::AddItem(const QString &category,
                 QWidget *parent) :
    QDialog(parent),
    ui(new Ui::AddItem)
{
    ui->setupUi(this);

    editFlag=false;
    _category=category;
    model=new QStandardItemModel(this);
    model->setHorizontalHeaderLabels(QStringList() << "属性" << "值");
    ui->tableView->setModel(model);
}

void AddItem::prepare(const QStringList &properties,
                      const QStringList &columns)
{
    if(properties.isEmpty())
        return;

    _properties=properties;
    if(columns.isEmpty() || properties.size() != columns.size() || columns.first().isEmpty())
    {
        foreach (QString property, properties) {
            QList<QStandardItem *> row={
                new QStandardItem(property),
                new QStandardItem(QString())
            };
            model->appendRow(row);
        }
    }
    else {
        for (int i = 0; i < columns.size(); ++i) {
            _map.insert(properties.at(i),columns.at(i));
            QList<QStandardItem *> row={
                new QStandardItem(columns.at(i)),
                new QStandardItem(QString())
            };
            model->appendRow(row);
        }
    }
}

void AddItem::setItem(const QStringList &properties,
                      const QStringList &values,
                      const QStringList &columns)
{
    if(properties.isEmpty() || values.isEmpty() || properties.size() != values.size())
        return;

    _properties=properties;
    _values=values;
    editFlag=true;
    ui->btn_submit->setText("保存");

    if(columns.isEmpty() || properties.size() != columns.size() || columns.first().isEmpty())
    {
        for (int i = 0; i < properties.size(); ++i) {
            QList<QStandardItem *> row={
                new QStandardItem(properties.at(i)),
                new QStandardItem(values.value(i))
            };
            model->appendRow(row);
            model->item(i)->setEditable(false);
        }
    }
    else {
        for (int i = 0; i < columns.size(); ++i) {
            _map.insert(properties.at(i),columns.at(i));
            QList<QStandardItem *> row={
                new QStandardItem(columns.at(i)),
                new QStandardItem(values.value(i))
            };
            model->appendRow(row);
            model->item(i)->setEditable(false);
        }
    }

    if(model->rowCount() > 0)
        model->item(0,1)->setEditable(false);
}

AddItem::~AddItem()
{
    delete ui;
}

void AddItem::on_btn_submit_clicked()
{
    if(model->rowCount() < 1 || model->item(0,1)->text().trimmed().isEmpty())
        return;

    QJsonObject obj;
    obj.insert("category",_category);

    if(!editFlag)
    {
        for (int i = 0; i < model->rowCount(); ++i) {
            QString property=model->item(i,0)->text();
            if(!_map.isEmpty())
                property=_map.key(property);

            QString value=model->item(i,1)->text().trimmed();
            obj.insert(property,value);
        }
        emit addItem(obj);
    }
    else {
        obj.insert(model->item(0,0)->text(),
                   model->item(0,1)->text());

        QJsonObject updater;
        for (int i = 1; i < model->rowCount(); ++i) {
            QString property=model->item(i,0)->text();
            if(!_map.isEmpty())
                property=_map.key(property);

            QString value=model->item(i,1)->text().trimmed();
            if(value != _values.value(i))
                updater.insert(property,value);
        }

        if(updater.isEmpty())
            return;

        QJsonArray array;
        array.append(obj);
        array.append(updater);

        emit updateItem(array);
    }
    accept();
}
